package com.wuyi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuyi.mapper.CollectMapper;
import com.wuyi.mapper.FollowMapper;
import com.wuyi.mapper.ResourceMapper;
import com.wuyi.mapper.UserMapper;
import com.wuyi.pojo.*;
import com.wuyi.pojo.vo.ResourceVO;
import com.wuyi.service.IndexService;
import com.zc.utils.JSONResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName indexServiceImpl.java
 * @Description 首页信息实现类
 * @createTime 2021年11月02日 23:20:00
 */
@Service
public class IndexServiceImpl implements IndexService {

    final static Logger logger = LoggerFactory.getLogger(IndexServiceImpl.class);

    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private UserMapper userMapper;



    @Transactional( propagation = Propagation.SUPPORTS)
    @Override
    public PageInfo catList(Integer categoryId, String userName, Integer  pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(Resource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("resourceCid", categoryId);
        example.orderBy("resourceTime").desc();
        List<Resource> list = resourceMapper.selectByExample(example);

        List<ResourceVO> resourceVOList = new ArrayList<>();


        //设置发布人id
        List<User> users = userMapper.selectAll();
        for (Resource resource : list) {
            for (User user : users) {
                if (resource.getResourceUid().equals(user.getUserName())) {
                    ResourceVO resourceVO = new ResourceVO();
                    BeanUtils.copyProperties(resource, resourceVO);
                    resourceVO.setResourceUserName(user.getUserName());
                    resourceVO.setResourceUid(user.getUserId());
                    resourceVO.setIsFollowed(0);
                    resourceVO.setIsCollected(0);
                    resourceVOList.add(resourceVO);
                    break;
                }
            }
        }

        //若登录，则展示收藏关注状态、没有则默认都为非关注收藏
        if (!StringUtils.isBlank(userName)) {
            //判断收藏表，与当前登录的用户判断
            List<Collect> collectList = collectMapper.selectAll();
            for (ResourceVO resourceVO : resourceVOList) {
                for (Collect collect : collectList) {
                    //若收藏表中的userName与当前登录用户的userName一致
                    //且收藏表中的资源ID与vo里的资源ID一致，则表示已收藏
                    if (collect.getCollectUid().equals(userName) &&
                            resourceVO.getResourceId().equals(collect.getCollectRid())) {
                        resourceVO.setIsCollected(1);
                    }
                }
            }
            //判断关注表，与当前登录的用户判断
            List<Follow> followList = followMapper.selectAll();
            for (ResourceVO resourceVO : resourceVOList) {
                for (Follow follow : followList) {
                    //若关注表中的userName与当前登录用户的userName一致
                    //且关注表中的被关注人userName与vo里的userName一致，则表示已关注
                    if (follow.getFollowUid().equals(userName) &&
                            resourceVO.getResourceUserName()
                                    .equals(follow.getFollowedUid())) {
                        resourceVO.setIsFollowed(1);
                    }
                }
            }
        }




        PageInfo pageInfo = new PageInfo<>(resourceVOList);
        pageInfo.setList(resourceVOList);
        return pageInfo;
    }

   /* @Transactional( propagation = Propagation.SUPPORTS)
    @Override
    public PageInfo catListTest(Integer categoryId, String userName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(Resource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("resourceCid", categoryId);
        example.orderBy("resourceTime").desc();
        List<Resource> list = resourceMapper.selectByExample(example);

        List<ResourceVO> resourceVOList = new ArrayList<>();


        //设置发布人id
        List<User> users = userMapper.selectAll();
        for (Resource resource : list) {
            for (User user : users) {
                if (resource.getResourceUid().equals(user.getUserName())) {
                    ResourceVO resourceVO = new ResourceVO();
                    BeanUtils.copyProperties(resource, resourceVO);
                    resourceVO.setResourceUserName(user.getUserName());
                    resourceVO.setResourceUid(user.getUserId());
                    resourceVO.setIsFollowed(0);
                    resourceVO.setIsCollected(0);
                    resourceVOList.add(resourceVO);
                    break;
                }
            }
        }
        //若登录，则展示收藏关注状态、没有则默认都为非关注收藏
        if (!StringUtils.isBlank(userName)) {
            //判断收藏表，与当前登录的用户判断
            List<Collect> collectList = collectMapper.selectAll();
            for (ResourceVO resourceVO : resourceVOList) {
                for (Collect collect : collectList) {
                    //若收藏表中的userName与当前登录用户的userName一致
                    //且收藏表中的资源ID与vo里的资源ID一致，则表示已收藏
                    if (collect.getCollectUid().equals(userName) &&
                            resourceVO.getResourceId().equals(collect.getCollectRid())) {
                        resourceVO.setIsCollected(1);
                    }
                }
            }

            //判断关注表，与当前登录的用户判断
            List<Follow> followList = followMapper.selectAll();
            for (ResourceVO resourceVO : resourceVOList) {
                for (Follow follow : followList) {
                    //若关注表中的userName与当前登录用户的userName一致
                    //且关注表中的被关注人userName与vo里的userName一致，则表示已关注
                    if (follow.getFollowUid().equals(userName) &&
                            resourceVO.getResourceUserName()
                                    .equals(follow.getFollowedUid())) {
                        resourceVO.setIsFollowed(1);
                    }
                }
            }
        }



        PageInfo pageInfo = new PageInfo<>(resourceVOList);
        pageInfo.setList(resourceVOList);
        return pageInfo;
    }*/

    /**
     * 首页按权重查询资源：推荐
     */
    @Override
    public List<Resource> recommend() {
        return null;
    }

    /**
     * 首页查询帖子：按日期排序
     */
    @Override
    public List<Post> queryPostList() {
        return null;
    }
}
